package com.czwj.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Duty2;
import com.czwj.entities.Duty3;
import com.czwj.entities.Duty4;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.ProjectItemChild;
import com.czwj.entities.Role;
import com.czwj.entities.UserInfo;
import com.czwj.service.Duty2Service;
import com.czwj.service.Duty3Service;
import com.czwj.service.Duty4Service;
import com.czwj.service.ProjectItemChildService;
import com.czwj.service.ProjectItemService;
import com.czwj.util.CipherUtil;
import com.czwj.util.TextUtil;

@Controller
public class ProjectItemAction {
	@Resource
	ProjectItemService projectItemService;
	@Resource
	ProjectItemChildService projectItemChildService;
	@Resource
	Duty2Service duty2Service;
	@Resource
	Duty3Service duty3Service;
	@Resource
	Duty4Service duty4Service;
	
	@RequestMapping(value = "/projectitemcompany",method = RequestMethod.POST)
	@ResponseBody
	public List<ProjectItem> projectitemcompany(HttpServletRequest request,HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		Subject currentUser = SecurityUtils.getSubject();
		int companyid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getCompanyid();
		List<ProjectItem> list = projectItemService.searchByCompany(companyid);
		return list;
	}
	
	@RequestMapping(value = "level0/projectitems.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<ProjectItem> projectitems0(HttpServletResponse response,
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate)  {
		DataGridData<ProjectItem> result = new DataGridData<>();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows))
		{
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status", status);
		}
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate", startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate", enddate);
		}
		Subject currentUser = SecurityUtils.getSubject();
		map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
		List<ProjectItem> list = projectItemService.searchforlevel0(map);
		result.setRows(list);
		result.setTotal(projectItemService.countforlevel0(map));
		return result;
	}
	
	@RequestMapping(value = "level1/projectitems.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<ProjectItem> projectitems1(HttpServletResponse response,
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate)  {
		DataGridData<ProjectItem> result = new DataGridData<>();
		Map<String, Object> map = new HashMap<String, Object>();
		Subject currentUser = SecurityUtils.getSubject();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows))
		{
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status", status);
		}
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate", startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate", enddate);
		}
		map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
		List<ProjectItem> list = projectItemService.searchforlevel1(map);
		result.setRows(list);
		result.setTotal(projectItemService.countforlevel1(map));
		return result;
	}
	
	@RequestMapping(value = "level2/projectitems.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<ProjectItem> projectitems2(HttpServletResponse response,
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate)  {
		DataGridData<ProjectItem> result = new DataGridData<>();
		Map<String, Object> map = new HashMap<String, Object>();
		Subject currentUser = SecurityUtils.getSubject();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows))
		{
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status", status);
		}
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate", startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate", enddate);
		}
		map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
		List<ProjectItem> list = projectItemService.searchforlevel2(map);
		result.setRows(list);
		result.setTotal(projectItemService.countforlevel2(map));
		return result;
	}
	
	@RequestMapping(value = "level3/projectitems.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<ProjectItem> projectitems3(HttpServletResponse response,
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate)  {
		DataGridData<ProjectItem> result = new DataGridData<>();
		Map<String, Object> map = new HashMap<String, Object>();
		Subject currentUser = SecurityUtils.getSubject();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows))
		{
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status", status);
		}
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate", startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate", enddate);
		}
		map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
		List<ProjectItem> list = projectItemService.searchforlevel3(map);
		result.setRows(list);
		result.setTotal(projectItemService.countforlevel3(map));
		return result;
	}
	
	@RequestMapping(value = "projectitemlist.do", method = RequestMethod.POST)
	@ResponseBody
	public List<ProjectItem> projectItemList(@RequestParam(value = "flg", required = false) Boolean flg,
			@RequestParam(value = "status", required = false) Integer status){
		Map<String, Object> map = new HashMap<String, Object>();
		if(status!=null){
			map.put("status", status);
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		if(userInfo.getRoleid()==1){
			map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			List<ProjectItem> list = projectItemService.searchforlevel0(map);
			if(flg!=null &&flg){
				ProjectItem item = new ProjectItem();
				item.setId(0);
				item.setName("全部");
				list.add(0, item);
			}
			return list;
			
		} else if(userInfo.getRoleid()==2){
			map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			List<ProjectItem> list = projectItemService.searchforlevel1(map);
			if(flg!=null &&flg){
				ProjectItem item = new ProjectItem();
				item.setId(0);
				item.setName("全部");
				list.add(0, item);
			}
			return list;
		}else if (userInfo.getRoleid()==3) {
			map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			List<ProjectItem> list = projectItemService.searchforlevel2(map);
			if(flg!=null &&flg){
				ProjectItem item = new ProjectItem();
				item.setId(0);
				item.setName("全部");
				list.add(0, item);
			}
			return list;
		}else if (userInfo.getRoleid()==4) {
			map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			List<ProjectItem> list = projectItemService.searchforlevel3(map);
			if(flg!=null &&flg){
				ProjectItem item = new ProjectItem();
				item.setId(0);
				item.setName("全部");
				list.add(0, item);
			}
			return list;
		}else {
			return null;
		}
	}
	
	@RequestMapping(value = "projectitemuserlist.do", method = RequestMethod.POST)
	@ResponseBody
	public List<ProjectItem> projectitemuserlist(){
		List<ProjectItem> projectItems = new ArrayList<>();
		List<Integer> ids = new ArrayList<>();
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		int roleid = userInfo.getRoleid();
		if(roleid==1||roleid==2){
			List<ProjectItem> list = projectItemService.searchByCreater(userInfo.getId());
			for(ProjectItem projectItem:list){
				ids.add(projectItem.getId());
			}
		}else if (roleid==3) {
			List<Duty2> list = duty2Service.searchByUser(userInfo.getId());
			for(Duty2 duty2:list){
				ids.add(duty2.getProjectitemid());
			}
		}else if (roleid==4) {
			List<Duty3> list = duty3Service.searchByUser(userInfo.getId());
			for(Duty3 duty3:list){
				ids.add(duty3.getProjectitemid());
			}
		}else if (roleid==6) {
			List<Duty4> list = duty4Service.searchByUser(userInfo.getId());
			for(Duty4 duty4:list){
				ids.add(duty4.getProjectitemid());
			}
		}else{
			List<ProjectItem> list = projectItemService.search();
			for(ProjectItem projectItem:list){
				ids.add(projectItem.getId());
			}
		}
		for(Integer integer:ids){
			ProjectItem projectItem = projectItemService.selectByPrimaryKey(integer);
			projectItems.add(projectItem);
		}
		return projectItems;
	}
	
	@RequestMapping(value = "/app/projectitemlist.do", method = RequestMethod.POST)
	@ResponseBody
	public List<ProjectItem> appprojectItemList(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 2);
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		if(userInfo.getRoleid()==1){
			map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			List<ProjectItem> list = projectItemService.searchtypeforlevel0(map);
			return list;
		}else if (userInfo.getRoleid()==2) {
			map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			List<ProjectItem> list = projectItemService.searchtypeforlevel1(map);
			return list;
		}
		else if (userInfo.getRoleid()==3) {
			map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			List<ProjectItem> list = projectItemService.searchtypeforlevel2(map);
			return list;
		}else if (userInfo.getRoleid()==4) {
			map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			List<ProjectItem> list = projectItemService.searchtypeforlevel3(map);
			return list;
		}else {
			return null;
		}
	}
	
	@RequestMapping(value = "unfinishprojectitemlist.do", method = RequestMethod.POST)
	@ResponseBody
	public List<ProjectItem> unfinishprojectItemList(){
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		List<ProjectItem> list = new ArrayList<>();
		if(userInfo.getRoleid()==1){
			list = projectItemService.projectitemswithoutfinishapply1(userInfo.getId());
		}else if (userInfo.getRoleid()==2) {
			list = projectItemService.projectitemswithoutfinishapply2(userInfo.getId());
		}else if (userInfo.getRoleid()==3) {
			list = projectItemService.projectitemswithoutfinishapply3(userInfo.getId());
		}else if (userInfo.getRoleid()==4) {
			list = projectItemService.projectitemswithoutfinishapply4(userInfo.getId());
		}
		
		return list;
	}
	
	@SystemControllerLog(description = "修改项目")
	@RequestMapping(value = "/ProjectItemSave.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult projectItemSave(@RequestBody ProjectItem item,HttpServletRequest request) throws Exception{
		OperateResult result = new OperateResult();
		int count = 0;
		if(TextUtil.isNotEmpty(item.getCompactmoney())){
			item.setCompactmoney(CipherUtil.encrypt(item.getCompactmoney()));
		}
		if (item.getId()!=null && item.getId()>0){
			count = projectItemService.update(item);
		}else{
			Subject currentUser = SecurityUtils.getSubject();
			item.setCreaterid(((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			item.setCreatedate(new Date());
			item.setCreateip(request.getRemoteAddr());
			count = projectItemService.insert(item);
		}
		result.setSuccess(count>0);
		return result;
	}
	
	@SystemControllerLog(description = "批准项目")
	@RequestMapping(value = "/ProjectItemConfirm.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult projectItemConfirm(@RequestParam(value = "id", required = true) int id){
		OperateResult result = new OperateResult();
		int count = 0;
		ProjectItem item = projectItemService.selectByPrimaryKey(id);
		if(item.getStatus() == 1){
			item.setStatus(2);
			count = projectItemService.updateByPrimaryKeySelective(item);
		}else{
			
		}
		result.setSuccess(count>0);
		return result;
	}
	
	@RequestMapping(value = "check/projectitemno", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult checkLoginname(@RequestParam(value = "projectno", required = true) String projectno,
			@RequestParam(value = "id", required = true) int id) {
		OperateResult result = new OperateResult();
		ProjectItem item = projectItemService.findByProjectNo(projectno);
		if(item ==null){
			result.setSuccess(true);
		}else{
			if(item.getId()==id){
				result.setSuccess(true);
			}else
			{
				result.setSuccess(false);
			}
		}
		return result;
	}
	
	@SystemControllerLog(description = "修改项目状态")
	@RequestMapping(value = "ProjectItemOff.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult projectItemOff(@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "status", required = false) Integer status) {
		OperateResult result = new OperateResult();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id",id);
		map.put("status",status);
		int count = projectItemService.updatestatus(map);
		if(status==3){
			List<ProjectItemChild> list = projectItemChildService.findbyfather(id);
			for(ProjectItemChild projectItemChild:list){
				Map<String, Object> map2 = new HashMap<String, Object>();
				map2.put("id",projectItemChild.getId());
				map2.put("status",status);
				projectItemChildService.updateStatus(map2);
			}
		}
		result.setSuccess(count>0);
		return result;
	}
	
	@RequestMapping(value = "encryptMoney.do", method = RequestMethod.GET)
	@ResponseBody
	public String encryptMoney(@RequestParam(value = "compactmoney", required = true) String money,
			@RequestParam(value = "projectitemid", required = true) int projectitemid) throws Exception{
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		if(userInfo.getRoleid()==1){
			return CipherUtil.decrypt(money);
		}else{
			ProjectItem item = projectItemService.selectByPrimaryKey(projectitemid);
			if(item.getCreaterid()==userInfo.getId()){
				return CipherUtil.decrypt(money);
			}else{
				return "";
			}
		}
	}
}
